/*
 * ParameterContext.java
 *
 * Created on September 6, 2006, 3:25 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package com.smallx.xproc.compiler;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.infoset.xml.Name;

/**
 *
 * @author alex
 */
public class ParameterContext extends HashMap<Name,Object> {

   Set<Name> keys;
   Map<Name,Object> parent;
   
   /** Creates a new instance of ParameterContext */
   public ParameterContext(Map<Name,Object> parent) {
      this.parent = parent;
      this.keys = null;
   }
   
   public void setParent(Map<Name,Object> parent) {
      this.parent = parent;
      this.keys = null;
   }
   
   public Object get(Name nm) {
      Object value = super.get(nm);
      if (value==null && parent!=null) {
         value = parent.get(nm);
      }
      return value;
   }
   
   public Object put(Name nm,Object value) {
      this.keys = null;
      return super.put(nm,value);
   }
   
   public Set<Name> keySet() {
      if (keys==null) {
         keys = new HashSet<Name>();
         if (parent!=null) {
            keys.addAll(parent.keySet());
         }
         keys.addAll(super.keySet());
      }
      return keys;
   }
   
   
}
